package com.hxzy.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(value = "/common/file")
public class UploadController {

    //文件保存路径
    @Value(value = "${mall.imageserverpath}")
    private String saveFilePath;

    //服务器访问项目名称
    @Value(value = "${mall.imageserver}")
    private String imageserver;
    /**
     *  文件上传
     * @param attach
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/upload")
    public ResultVO  upload(MultipartFile attach) throws IOException {
        ResultVO  resultVO=null;
        //如果文件是空的
        if(attach.isEmpty()){
            resultVO=ResultVO.failed(ResultEnum.FILE_NOT_EMPTY);
            return resultVO;
        }

        //保存，上传文件后缀名
        String originalFilename = attach.getOriginalFilename();
        String extension= originalFilename.substring(originalFilename.lastIndexOf("."));

        //生成新的名称 UUID
        String fileName= UUID.randomUUID().toString()+extension;

        //D:\mall-image \ecfe92d9-fd99-4d35-8a3b-6ea856019470.jpg
        //新建一个文件  File saveFile=new File("目录","文件名称");
        File saveFile=new File(saveFilePath,fileName);
        //使用内置工作复制文件
        attach.transferTo(saveFile);

        //输出给前端
        String url=imageserver+"/"+fileName;
        resultVO=ResultVO.success(url);

        return resultVO;
    }


    /**
     *  文件上传
     * @param multipartRequest
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/upload/editor")
    public JSONObject  uploadWangEditor( MultipartRequest multipartRequest) throws IOException {
        //数据
        List<Map<String,String>>  data=new ArrayList<>();

        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();

        for(String key : fileMap.keySet()) {
            MultipartFile attach=fileMap.get(key);
            if (!attach.isEmpty()) {
                // {url:'地址'}
                Map<String, String> resultMap = uploadWangEditorSave(attach);
                data.add(resultMap);
            }
        }


       // resultVO=ResultVO.success(url);
        JSONObject  resultVo=new JSONObject();
        resultVo.put("errno",0);
        resultVo.put("data", data);
        return resultVo;
    }

    private Map<String,String>  uploadWangEditorSave(MultipartFile  attch) throws IOException {

        //保存，上传文件后缀名
        String originalFilename = attch.getOriginalFilename();
        String extension= originalFilename.substring(originalFilename.lastIndexOf("."));

        //生成新的名称 UUID
        String fileName= UUID.randomUUID().toString()+extension;

        //D:\mall-image \ecfe92d9-fd99-4d35-8a3b-6ea856019470.jpg
        //新建一个文件  File saveFile=new File("目录","文件名称");
        File saveFile=new File(saveFilePath,fileName);
        //使用内置工作复制文件
        attch.transferTo(saveFile);

        //输出给前端
        String url=imageserver+"/"+fileName;

        Map<String,String>  mp=new HashMap<>();
        mp.put("url", url);
        return mp;
    }

}
